package battleOfTheSevenSeas.Serveur;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Connexion extends Thread{
	protected Socket client;
	protected BufferedReader datain;
	protected DataOutputStream dataout;

	public Connexion(Socket connectionSocket) {

		client = connectionSocket;
		//		System.out.println("we're inside");
		try {
			datain = new BufferedReader (new InputStreamReader(client.getInputStream()));
			dataout = new DataOutputStream(client.getOutputStream());
		}
		catch (IOException ioe){
			try {
				client.close();
			}
			catch (IOException e){				
			}
			System.err.println(ioe.getMessage());
			return;
		}
		//		System.out.println("avant start");
		this.start();
	}

	public void run(){
		try {
			while(true){
				String ligne;
				ligne = datain.readLine();

				String[] res = ligne.split("/");
				int taille = res.length;
				System.out.println(taille);
				for (int i = 0; i < taille; i++){
					System.out.println(res[i]);
				}
				String req = "";
				//				System.out.println("avant test");
				if (res[0].compareToIgnoreCase("connect") == 0 & res.length == 2){
					req = "WELCOME/"+ res[1] + "/";
					//					System.out.println(req);
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("spectator") == 0 & res.length == 1){
					req = "WELCOME/spectator/";
					//					System.out.println(req);
					dataout.writeBytes(req + "\n");
					dataout.flush();
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataout.writeBytes("HEYLISTEN/Toto/Say my name !/" + "\n");
					dataout.flush();
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataout.writeBytes("HEYLISTEN/LesArgonauts/Say my name !/" + "\n");
					dataout.flush();
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataout.writeBytes("HEYLISTEN/Tata/Say my name !/" + "\n");
					dataout.flush();
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataout.writeBytes("HEYLISTEN/Donald/Say my name !/" + "\n");
					dataout.flush();
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataout.writeBytes("HEYLISTEN/LordVoldemort/Say my name !/" + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("aph") == 0 & res.length == 1){
					req = "PLAYERS/toto/titi/tata/donald/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("debutship") == 0 & res.length == 1){
					req = "SHIP/4/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataout.writeBytes("HEYLISTEN/Toto/Say my name !/" + "\n");
					dataout.flush();
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataout.writeBytes("HEYLISTEN/LesArgonauts/Say my name !/" + "\n");
					dataout.flush();
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataout.writeBytes("HEYLISTEN/Tata/Say my name !/" + "\n");
					dataout.flush();
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataout.writeBytes("HEYLISTEN/Donald/Say my name !/" + "\n");
					dataout.flush();
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					dataout.writeBytes("HEYLISTEN/LordVoldemort/Say my name !/" + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("talk") == 0 & res.length == 2){
					req = "HEYLISTEN/LordVoldemort/" + res[1] + "/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("putship") == 0 & res.length == 9){
					
					req = "OK/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("ayb") == 0 & res.length == 1){
					req = "ALLYOURBASE/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("yt") == 0 & res.length == 1){
					req = "YOURTURN/0/P/5/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("action") == 0){
					if (res[1].compareToIgnoreCase("E") == 0){
						req = "TOUCHE/8/G/";
					}else if (res[1].compareToIgnoreCase("D") == 0){
						req = "MISS/8/G/";
					}
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("ouch") == 0 & res.length == 1){
					req = "OUCH/3/D/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("death") == 0 & res.length == 1){
					req = "DEATH/toto/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("win") == 0 & res.length == 1){
					req = "AWINNERIS/titi/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("draw") == 0 & res.length == 1){
					req = "DRAWGAME/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("playagain") == 0 & res.length == 1){
					req = "WELCOME/titi/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("bye") == 0 & res.length == 1){
					client.close();
				}else if (res[0].compareToIgnoreCase("register") == 0 & res.length == 3){
					System.out.println(res[1] + res[2]);
					//					req = "bye";
					//					dataout.writeBytes(req + "\n");
					//					dataout.flush();
				}else if (res[0].compareToIgnoreCase("ps") == 0 ){
					req = "PLAYERSHIP/toto/5/G/6/G/7/G/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("pm") == 0 ){
					req = "PLAYERMOVE/toto/8/G/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("po") == 0 ){
					req = "PLAYEROUCH/toto/5/G/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("ps1") == 0 ){
					req = "PLAYERSHIP/tata/5/E/6/E/7/E/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("pm1") == 0 ){
					req = "PLAYERMOVE/tata/3/D/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else if (res[0].compareToIgnoreCase("po1") == 0 ){
					req = "PLAYEROUCH/tata/10/G/";
					dataout.writeBytes(req + "\n");
					dataout.flush();

				}else if (res[0].compareToIgnoreCase("login") == 0 & res.length == 3){
					req = "WELCOME/" + res[1] + "/";
					dataout.writeBytes(req + "\n");
					dataout.flush();
				}else{
					//					System.out.println(ligne.toUpperCase() + "else");
					dataout.writeBytes("ACCESSDENIED" + "\n");
					dataout.flush();
				}
				//				System.out.println("ending");
			}
		} catch (NullPointerException e){
			System.out.println("Connexion : " + e.toString());
			System.exit(1);
		} catch (IOException e){
			System.out.println("Connexion : " + e.toString());
			System.exit(1);
		} finally {
			try {
				client.close();
			} catch (IOException e){}				
		}


	}
}


